#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using Gizmox.WebGUI.Forms;
using TVC.Layout;

#endregion

namespace TVC.Modules.Ddb
{
    public partial class frmGadgetConfig : Form
    {
        string sErr = "";
        String _TypeChart = "";

        public MyGadget Gadget { get; set; }

        public MainForm MainForm { get; set; }
        public String TypeChart
        {
            get { return _TypeChart; }
            set { _TypeChart = value; }
        }

        public frmGadgetConfig(MainForm form)
        {
            InitializeComponent();
            dgListChart.AutoGenerateColumns = false;
            dgListChart.DataSource = bisChart;
            MainForm = form;
        }

        public frmGadgetConfig(MyGadget gadget, MainForm from)
        {
            InitializeComponent();
            dgListChart.AutoGenerateColumns = false;
            dgListChart.DataSource = bisChart;
            Gadget = gadget;
            MainForm = from;
            //LoadListChart();
        }
        public frmGadgetConfig(Gizmox.WebGUI.Common.Interfaces.IContext objContext)
            : base(objContext)
        {

        }


        //private void LoadListChart()
        //{
        //    if (_gadget.Info.ID != -1)
        //    {
        //        BUS.LIST_CHARTControl ctr = new BUS.LIST_CHARTControl();
        //        DataTable dt = ctr.Get_ByIDGadget(MainForm.DB, _gadget.Info.ID, ref sErr);
        //        dgListChart.DataSource = dt;
        //    }
        //    else
        //        dgListChart.DataSource = null;
        //}

        private int GetLastIDGadget(int index)
        {
            BUS.LIST_GADGETControl ctr = new BUS.LIST_GADGETControl();
            int i = ctr.GetCountRecord(MainForm.DB, ref sErr) + index;
            if (ctr.IsExist(MainForm.DB, i))
            {
                index++;
                i = GetLastIDGadget(index);
            }

            return i;
        }

        private int GetLastIDChart(int index)
        {
            BUS.LIST_CHARTControl ctrchart = new BUS.LIST_CHARTControl();
            int i = ctrchart.GetCountRecord(MainForm.DB, ref sErr) + index;
            if (ctrchart.IsExist(MainForm.DB, i))
            {
                index++;
                i = GetLastIDChart(index);
            }

            return i;
        }

        private void UpdateUndefault()
        {
            BUS.LIST_GADGETControl ctr = new BUS.LIST_GADGETControl();
            DataTable dt = ctr.Get_ByDefault(MainForm.DB, Gadget.Info.Owner, Gadget.Info.Type, ref sErr);

            foreach (DataRow row in dt.Rows)
            {
                row["IsDefault"] = false;
                DTO.LIST_GADGETInfo iinf = new DTO.LIST_GADGETInfo(row);
                ctr.Update(iinf);
            }

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            BUS.LIST_GADGETControl ctr = new BUS.LIST_GADGETControl();
            BUS.LIST_CHARTControl ctrchart = new BUS.LIST_CHARTControl();
            //UpdateUndefault();
            GetDataFromForm();

            if (Gadget.Info.ID == -1)
            {
                Gadget.Info.ID = GetLastIDGadget(1);
                Gadget.Info.DTB = MainForm.DB;

                ctr.Add(Gadget.Info, ref sErr);
                for (int i = 0; i < Gadget.Charts.Count; i++)
                {
                    MyChart temp = Gadget.Charts[i];
                    temp.IDGadget = Gadget.Info.ID;
                    temp.DTB = MainForm.DB;
                    temp.ID = GetLastIDChart(1);
                    ctrchart.Add(temp.ChartInf, ref sErr);
                }
            }
            else
            {
                ctr.InsertUpdate(Gadget.Info);

                ctrchart.Delete_byGadget(MainForm.DB, Gadget.Info.ID);
                for (int i = 0; i < Gadget.Charts.Count; i++)
                {
                    MyChart temp = Gadget.Charts[i];
                    temp.IDGadget = Gadget.Info.ID;
                    temp.DTB = MainForm.DB;
                    temp.ID = GetLastIDChart(1);
                    ctrchart.Add(temp.ChartInf, ref sErr);
                }
            }
            DialogResult = DialogResult.OK;
            Reset();
            Close();

        }

        private void Reset()
        {
            txtRTPPrint.Text = "";
            txtDescription.Text = "";
            mcbQueryCode.Value = "";
            txtReportTemplate.Text = "";
            cbDefault.Checked = false;
        }

        private void LoadGadgetInfo(DTO.LIST_GADGETInfo info)
        {
            txtID.Text = info.ID.ToString();
            txtOwner.Text = info.Owner;
            ckbShared.Checked = info.Shared;
            mcbDataGroup.Value = info.DataGroup;
            cboGadgetType.Text = info.Type;
            txtDescription.Text = info.Description;
            mcbQueryCode.Value = info.QDCode;
            txtReportTemplate.Text = info.ReportTmp;
            txtRTPPrint.Text = info.ReportTmp_Print;
            cbDefault.Checked = info.IsDefault;
            LoadGridChart();
        }

        private void LoadGridChart()
        {
            bisChart.DataSource = Gadget.Charts;
        }
        private void GetDataFromForm()
        {
            Gadget.Info.ID = Convert.ToInt32(txtID.Text);
            Gadget.Info.Description = txtDescription.Text;
            Gadget.Info.QDCode = mcbQueryCode.Value;
            Gadget.Info.ReportTmp = txtReportTemplate.Text;
            Gadget.Info.ReportTmp_Print = txtRTPPrint.Text;
            Gadget.Info.IsDefault = cbDefault.Checked;
            Gadget.Info.Owner = txtOwner.Text;
            Gadget.Info.Shared = ckbShared.Checked;
            Gadget.Info.DataGroup = mcbDataGroup.Value;
            Gadget.Info.Type = cboGadgetType.Text;
        }

        private void LoadDataToForm()
        {

            if (Gadget.Charts.Count == 0)
            {
                BUS.LIST_CHARTControl ctrchart = new BUS.LIST_CHARTControl();
                DataTable dt = ctrchart.Get_ByIDGadget(MainForm.DB, Gadget.Info.ID, ref sErr);

                if (dt.Rows.Count > 0)
                {
                    MyChart chart = new MyChart() { ChartInf = new DTO.LIST_CHARTInfo(dt.Rows[0]) };
                    Gadget.Charts.Add(chart);
                    //LoadChartInfo(chart);
                }

            }
            LoadGadgetInfo(Gadget.Info);

        }

        private void btnQD_Click(object sender, EventArgs e)
        {
            Forms.frmView frm = new Forms.frmView("QD", MainForm);

            frm.FormClosed += (form, args) =>
            {
                if (((Forms.frmView)form).DialogResult == DialogResult.OK)
                {
                    mcbQueryCode.Value = ((Forms.frmView)form).ReturnValue[((Forms.frmView)form).ReturnValue.Table.PrimaryKey[0]].ToString();
                }
            };
            frm.ShowDialog();



        }


        private void frmGadgetConfig_Load(object sender, EventArgs e)
        {
            Reset();
            if (!MainForm.IsContainsTables("DA"))
                MainForm.LoadDataTable("DA");
            mcbDataGroup.Binding = MainForm.GetTable("DA");
            GridLayout alayout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, MainForm.PODInfo.USER_ID), "DA");
            MainForm.InitMultiComboBox(mcbDataGroup, alayout, DTO.LIST_DAInfo.Field.NAME.ToString(), DTO.LIST_DAInfo.Field.DAG_ID.ToString());

            if (!MainForm.IsContainsTables("QD"))
                MainForm.LoadDataTable("QD");
            mcbQueryCode.Binding = MainForm.GetTable("QD");
            GridLayout blayout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, MainForm.PODInfo.USER_ID), "QD");
            MainForm.InitMultiComboBox(mcbQueryCode, blayout, "DESCRIPTN", "QD_ID");
            LoadDataToForm();
        }



        private void LoadChartInfo(MyChart inf)
        {
            //DTO.LIST_CHARTInfo inf = chart.ChartInf;
            cbbType.Text = inf.ChartType.Trim();

            txtCaptionRange.Text = inf.CaptionRange;
            txtSubCaptionRange.Text = inf.SubCaptionRange;
            txtSheetChart.Text = inf.SheetChart;
            txtNameRange.Text = inf.NameRange;
            txtPSReplace.Text = inf.PositionReplace;
            txtwidth.Text = inf.width.ToString();
            txtheight.Text = inf.height.ToString();
        }



        private void btnAdd_Click(object sender, EventArgs e)
        {
            BindingList<MyChart> list = bisChart.DataSource as BindingList<MyChart>;
            if (list != null)
            {
                for (int i = 0; i < numericUpDown2.Value; i++)
                    list.Add(new MyChart());
                bisChart.DataSource = list;
            }
        }

        //private void dgListChart_CurrentCellChanged(object sender, EventArgs e)
        //{
        //    if (dgListChart.CurrentCell != null)
        //    {
        //        MyChart inft = (MyChart)dgListChart.CurrentRow.DataBoundItem;
        //        LoadChartInfo(inft);
        //    }
        //}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }






    }
}